package com.ql.gamelogm.controller;

import com.ql.gamelogm.entities.*;
import com.ql.gamelogm.service.DataLogService;
import com.ql.gamelogm.service.LogService;
import com.ql.gamelogm.service.impl.LogServiceImpl;
import com.ql.gamelogm.utils.IpUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class LogController {
    @Autowired
    private LogServiceImpl logServiceImpl;
    @Autowired
    private LogService logService;
    @Autowired
    private DataLogService dataLogService;


    @GetMapping(value = "/findAllLogs")
    public String list(HttpSession httpSession,Model model, @RequestParam(value = "pageNum",defaultValue = "0")int PageNum, @RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<Log> logList=logServiceImpl.getLogList(PageNum,PageSize,operator);
        model.addAttribute("logList", logList);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("操作日志记录查询");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "UserOplog";
    }

    @GetMapping(value = "/LogCatchPre")
    public String searchHistory(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<SearchHistory> searchHistories=logServiceImpl.getAllSearchHisroty(PageNum,PageSize,operator);
        model.addAttribute("searchHistories",searchHistories);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("日志搜索历史查询");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "LogCatch";
    }
    @GetMapping(value = "/findWaitLog")
    public String findwaitlog(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<WarnLog> waitWarnLogList=logServiceImpl.getWaitWarnLogList(PageNum,PageSize);
        model.addAttribute("waitWarnLogList",waitWarnLogList);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("待接收日志查询");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "LogWarnW";
    }
    @GetMapping(value = "/findacepLogs")
    public String findlogs(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<WarnLog> WarnLogList=logServiceImpl.getWarnLogList(PageNum,PageSize);
        model.addAttribute("WarnLogList",WarnLogList);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("已接收日志查询");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "LogWarnA";
    }
    @GetMapping(value = "/findSolvedLogs")
    public String findsolvedlogs(HttpSession httpSession,Model model,@RequestParam(value = "pageNum",defaultValue = "0")int PageNum,@RequestParam(value = "pageSize",defaultValue = "8")int
            PageSize) throws ParseException {
        String operator=(String)httpSession.getAttribute("id");
        Page<WarnLog> solvedWarnLogList=logServiceImpl.getSolvedWarnLogList(PageNum,PageSize);
        model.addAttribute("solvedWarnLogList",solvedWarnLogList);
        //查操作记录
        Date date=new Date();
        date.setTime(System.currentTimeMillis());
        String ipaddr= IpUnit.getIpAddr();
        Log log=new Log();
        log.setDate(date);
        log.setOperator(operator);
        log.setOperation("已处理日志查询");
        log.setIpaddr(ipaddr);
        logService.savelog(log);
        return "LogWarnS";
    }
   /* 日志接收*/
    @RequestMapping(value = "/getWarnLogs")
    public String getWaitLog(@RequestParam(value = "logid")String id,HttpServletRequest httpServletRequest , HttpSession httpSession) throws ParseException {
        String id1=(String)httpSession.getAttribute("id");
        int id2=Integer.parseInt(id);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
        List<WarnLog> warnLogList=logService.findWarnLogById(id2);
        Date date = warnLogList.get(0).getDate();
        Date curdate=new Date(System.currentTimeMillis());
        double diff=curdate.getTime()-date.getTime();
        double days = diff / (1000 * 60 * 60 * 24);
        BigDecimal b=new  BigDecimal(days);
        double   f1=b.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
        WarnLog warnLog=new WarnLog();
        warnLog.setState("已接收");
        warnLog.setAvgresponse(f1);
        warnLog.setSolveperson(id1);
        warnLog.setDate(simpleDateFormat.parse(simpleDateFormat.format(curdate)));
        warnLog.setDateres(simpleDateFormat1.parse(simpleDateFormat1.format(curdate)));
        boolean iao =logService.updateWaitWarnLogState(id2,warnLog);
        if(iao){
            //存添加用户操作记录
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(curdate);
            log.setOperator(id1);
            log.setOperation("接收日志");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
            httpSession.setAttribute("isaddok",iao);
        }
        return "redirect:/findWaitLog";
    }
    //日志处理
    @RequestMapping(value = "/acepWarnLogs")
    public String solveLog(@RequestParam(value = "logid")String id,HttpServletRequest httpServletRequest , HttpSession httpSession) throws ParseException {
        String id1=(String)httpSession.getAttribute("id");
        int id2=Integer.parseInt(id);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
        List<WarnLog> warnLogList=logService.findWarnLogById(id2);
        Date date = warnLogList.get(0).getDate();
        Date curdate=new Date(System.currentTimeMillis());
        double diff=curdate.getTime()-date.getTime();
        double days = diff / (1000 * 60 * 60 * 24);
        BigDecimal b=new  BigDecimal(days);
        double f1=b.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();
        WarnLog warnLog=new WarnLog();
        warnLog.setState("已处理");
        warnLog.setAvgresponse(f1);
        warnLog.setSolveperson(id1);
        warnLog.setDate(simpleDateFormat.parse(simpleDateFormat.format(curdate)));
        warnLog.setDateres(simpleDateFormat1.parse(simpleDateFormat1.format(curdate)));
        boolean iao =logService.updateAcepWarnLogState(id2,warnLog);
        if(iao){
            //存添加用户操作记录
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(curdate);
            log.setOperator(id1);
            log.setOperation("处理日志");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
            httpSession.setAttribute("isaddok",iao);
        }
        return "redirect:/findacepLogs";
    }
    //添加告警日志
    @RequestMapping(value = "/addWarnLog")
    public String addWarnLog(HttpServletRequest httpServletRequest ,HttpSession httpSession) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
        String solveperson=(String)httpSession.getAttribute("id");
        String dateid=httpServletRequest.getParameter("dateid");
        List<ProblemData> problemDataList=dataLogService.findProblemLogs();
        Date date=new Date();
        try{
            int id=Integer.parseInt(dateid);
            for(int i=0;i<problemDataList.size();i++){
                if(problemDataList.get(i).getId()==id){
                    date = problemDataList.get(i).getDate();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String datestr=simpleDateFormat.format(date);
        Long currenttime=simpleDateFormat.parse(datestr).getTime()+1000;
        Date date1=new Date();
        date1.setTime(currenttime);
        logService.saveRecentTime(simpleDateFormat.format(date1));//得到最近分析时间
        String deg=httpServletRequest.getParameter("degree");
        String descr=httpServletRequest.getParameter("describe");
        String gamername=httpServletRequest.getParameter("gamername");
        String state="待接收";
        double avgrecovery=0.0;
        double avgresponse=0.0;
        Date curdate=new Date(System.currentTimeMillis());
        WarnLog warnLog=new WarnLog();
        warnLog.setState(state);
        warnLog.setDeg(deg);
        warnLog.setDescr(descr);
        warnLog.setGamername(gamername);
        warnLog.setAvgrecovery(avgrecovery);
        warnLog.setAvgresponse(avgresponse);
        warnLog.setSolveperson(solveperson);
        warnLog.setDate(simpleDateFormat.parse(simpleDateFormat.format(curdate)));
        warnLog.setDateres(simpleDateFormat1.parse(simpleDateFormat1.format(curdate)));
        boolean isw = logService.saveWarnLog(warnLog);
        if(isw){
            //存添加用户操作记录
            Date date2=new Date();
            date.setTime(System.currentTimeMillis());
            String ipaddr= IpUnit.getIpAddr();
            Log log=new Log();
            log.setDate(date2);
            log.setOperator(solveperson);
            log.setOperation("添加告警记录");
            log.setIpaddr(ipaddr);
            logService.savelog(log);
        }
        return "redirect:/findProblemDataLogs";
    }
}
